package com.xwh.shop.goods.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwh.shop.goods.model.entity.Category;
import com.xwh.shop.goods.model.vo.CategoryListVO;
import com.xwh.shop.goods.service.mapper.CategoryMapper;
import com.xwh.shop.goods.service.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwh
 * @since 2025-07-16
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public Page<Category> pageQuery(Integer current, Integer pageSize, String name, Byte isShow) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), Category::getName, name);
        wrapper.eq(isShow != null,Category::getIsShow,isShow);

        return this.page(new Page<>(current,pageSize),wrapper);
    }

    @Override
    public List<CategoryListVO> getList() {
        //显示所有的分类 (isShow = 1)
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getIsShow,1);
        // 降序
        wrapper.orderByDesc(Category::getSort);

        List<Category> pageList = this.list(wrapper);
        //将查询到的数据封装成CategoryListVO
        List<CategoryListVO> pageVOList = new ArrayList<>();
        for (Category category : pageList) {
            pageVOList.add(new CategoryListVO(category));
        }

        return pageVOList;

    }
}
